﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Lib.Utility;
using Module.BusinessLogic;
using Module.Core.LoginSystem.Commons.Framwork;
using Module.EntitiesInfo;

namespace Web.Cms.Advertistment
{
    public partial class AddEditAdvPostion : AdminPage
    {
        private int advPostion = 0;
        private AdvertisementPositionsInfo advertisementPositionsInfo;
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                advPostion = Convert.ToInt32(Request.QueryString["PositionID"]);
                if (!IsPostBack)
                {
                    BannerPos.Fill(dropPositions);
                    LoadData();
                   
                }
            }
            catch (Exception ex)
            {

                lblUpdateStatus.Text = "Lỗi" + ex.Message;
            }
        }
        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                advertisementPositionsInfo = new AdvertisementPositionsInfo();
                FillData(advertisementPositionsInfo);
                if (txtName.Text.Length == 0)
                {

                    MessageBox.Show("Bạn chưa nhập tên quảng cáo?");
                    txtName.Focus();
                    return;
                }
                if (txtWidth.Text.Length == 0)
                {

                    MessageBox.Show("Bạn chưa nhập tên quảng cáo?");
                    txtWidth.Focus();
                    return;
                }
                if (txtHeight.Text.Length == 0)
                {

                    MessageBox.Show("Bạn chưa nhập tên quảng cáo?");
                    txtHeight.Focus();
                    return;
                }
                else
                {
                    AdvertisementPositionsBL.Update(advertisementPositionsInfo);
                    lblUpdateStatus.Text = "Bạn đã chỉnh sửa thành công";  
                }
               
            }
            catch (Exception ex)
            {

                lblUpdateStatus.Text = "Lỗi" + ex.Message;
            }

        }
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                advertisementPositionsInfo = new AdvertisementPositionsInfo();
                FillData(advertisementPositionsInfo);
                if(txtName.Text.Length==0)
                {

                    MessageBox.Show("Bạn chưa nhập tên quảng cáo?");
                    txtName.Focus();
                    return;
                }
                if(txtWidth.Text.Length==0)
                {

                    MessageBox.Show("Bạn chưa nhập tên quảng cáo?");
                    txtWidth.Focus();
                    return;
                }
                if(txtHeight.Text.Length==0)
                {

                    MessageBox.Show("Bạn chưa nhập tên quảng cáo?");
                    txtHeight.Focus();
                    return;
                }
                else
                {
                    AdvertisementPositionsBL.Insert(advertisementPositionsInfo);
                    lblUpdateStatus.Text = "Bạn đã thêm mới thành công";
                }
               
            }
            catch (Exception ex)
            {
                lblUpdateStatus.Text = "Lỗi" + ex.Message;
            }
        }

        public void LoadData()
        {
            try
            {
                if (advPostion > 0)
                {
                    btnUpdate.Visible = true;
                    btnAdd.Visible = false;

                     advertisementPositionsInfo = AdvertisementPositionsBL.GetInfo(2, advPostion.ToString(),null);
                    if (advertisementPositionsInfo != null)
                    {
                        txtName.Text = advertisementPositionsInfo.Title;
                        txtDescription.Text = advertisementPositionsInfo.Description;
                        rdoShowMode.SelectedValue = advertisementPositionsInfo.ShowMode.ToString();
                        txtWidth.Text = advertisementPositionsInfo.Width.ToString();
                        txtHeight.Text = advertisementPositionsInfo.Height.ToString();
                        txtTimeDelay.Text = advertisementPositionsInfo.TimeDelay.ToString();
                        dropPositions.SelectedValue = advertisementPositionsInfo.GroupPostion.ToString();
                        btnUpdate.Visible = true;
                        btnAdd.Visible = false;
                    }
                  
                }
                else
                {
                    btnUpdate.Visible = false;
                    btnAdd.Visible = true;


                }
            }
            catch (Exception ex)
            {
                lblUpdateStatus.Text = "Có lỗi xảy ra Error: " + ex.Message;
            }
        }

        void FillData(AdvertisementPositionsInfo advertisementPositionsInfo)
        {
            advertisementPositionsInfo.PositionID = advPostion;
            advertisementPositionsInfo.Title = txtName.Text;
            advertisementPositionsInfo.Description = txtDescription.Text;
            advertisementPositionsInfo.ShowMode = ConvertUtility.ToInt32(rdoShowMode.SelectedValue);
            advertisementPositionsInfo.RepeatColumns = 1;
            advertisementPositionsInfo.CreatedBy = CurrentUser.UserID;
            advertisementPositionsInfo.CreatedOn = DateTime.Now;
            advertisementPositionsInfo.Width = ConvertUtility.ToInt32(txtWidth.Text);
            advertisementPositionsInfo.Height = ConvertUtility.ToInt32(txtHeight.Text);
            advertisementPositionsInfo.TimeDelay = float.Parse(txtTimeDelay.Text);
            advertisementPositionsInfo.Priority = 0;
            advertisementPositionsInfo.Lang = 1;
            advertisementPositionsInfo.GroupPostion = Convert.ToInt32(dropPositions.SelectedValue);
        }

        protected void btnCanel_Click(object sender, EventArgs e)
        {
            HttpContext.Current.Response.Write("<script>window.top.hidePopWinADMIN(false);window.parent.location.href='/Cms/Default.aspx?ctrlkey=AdvertisementPostionManager';</script>");
        }
    }
}